import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.InetAddress;

public class Control implements ActionListener, ItemListener
{
	private InetAddress bindAddress;
	private int port = 80;
	private String contentbase = System.getProperty("user.dir");

	private MyJFrame frame;
	private InetAddress[] inetaddresses;
	private Server s;
	private Thread serverThread;

	private int currentId = 0;

	public Control()
	{
		System.out.println("Control, Control()");  
		try
		{
			InetAddress localhost = InetAddress.getLocalHost();

			inetaddresses = InetAddress.getAllByName(localhost.getHostName());

			byte[] zeros = { 0, 0, 0, 0 };
			InetAddress inetaddresses1[] = new InetAddress[inetaddresses.length + 1];
			System.arraycopy(inetaddresses, 0, inetaddresses1, 0, inetaddresses.length);
			inetaddresses1[inetaddresses1.length - 1] = InetAddress.getByAddress(localhost.getHostName(), zeros);
			inetaddresses = inetaddresses1;

			int response = 0;

			bindAddress = inetaddresses[response];
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		} 
		finally
		{

		}

		frame = new MyJFrame(this);
	}

	public void doStart()
	{
		System.out.println("Control, doStart()");
		try
		{
			this.s = new Server(this.bindAddress, this.port, this);
			serverThread = new Thread(s);
			serverThread.start();
		}
		catch (Exception e)
		{
			System.err.println(e.getMessage());
		}
	}

	public void doStop()
	{
		serverThread.interrupt();
		s.stop();

		if(!s.isAlive()) {
			System.gc();
		}
	}

	public void log(String s, int id)
	{
		System.out.println("Control, log(String s, int id)");
		MyJLayeredPane mp = (MyJLayeredPane) frame.getLayeredPane();
		mp.getTextPane(id).append(s);
		frame.repaint();
	}

	public void doPort(int port)
	{
		System.out.println("Control, doPort(int port)");
		this.port = port;
	}

	public void doBindAddress(InetAddress ia)
	{
		System.out.println("Control, doBindAddress(InetAddress ia)");
		bindAddress = ia;
	}

	public void doContentBase(String cb)
	{
		System.out.println("Control, doContentBase(String cb)");	  
		this.contentbase = cb;
	}

	public void actionPerformed(ActionEvent arg0)
	{
		System.out.println("Control, actionPerformed(ActionEvent arg0");
		try
		{
			int port = Integer.parseInt(arg0.getActionCommand());
			doPort(port);
		}
		catch (Exception e)
		{
			doContentBase(arg0.getActionCommand());
		}
		finally
		{

		}
	}

	public void itemStateChanged(ItemEvent arg0)
	{
		System.out.println("Control, itemStateChanged(ItemEvent arg0)");  
		doBindAddress((InetAddress) arg0.getItem());
	}

	public InetAddress[] getInetAddresses()
	{
		System.out.println("Control, getInetAddresses()");
		return this.inetaddresses;
	}

	public String getContentBase()
	{
		System.out.println("Control, getContentBase()");
		return this.contentbase;
	}

	public void setCurrentId(int i)
	{
		System.out.println("Control, setCurrentId(int i)");  
		if(i > -1)
		{
			currentId = i;
		}
		//frame.getGlassPane().repaint();
	}

	public void addTextPane() {
		System.out.println("Control, addTextPane()");
		MyJLayeredPane mjp = (MyJLayeredPane) frame.getLayeredPane();
		mjp.addTextPane();
	}

	public int getCurrentId() {
		System.out.println("Control, getCurrentId()");
		return currentId;
	}

	protected void finalize() throws Throwable {
		serverThread.interrupt();
		s.close();
	}

	public String toString()
	{
		System.out.println("Request, toString()");
		return super.toString();
	}
}